package com.work.yshop.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.work.yshop.R;
import com.work.yshop.adapter.MessageAdapter;
import com.work.yshop.model.Message;
import com.work.yshop.ui.activity.ChatActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息页面Fragment
 */
public class MessageFragment extends Fragment {

    private static final int REQUEST_CHAT = 1001;
    
    private View rootView;
    private RecyclerView recyclerView;
    private MessageAdapter adapter;
    private List<Message> messages = new ArrayList<>();

    public static MessageFragment newInstance() {
        return new MessageFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_message, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
        setupRecyclerView();
        loadMessages();
    }

    /**
     * 初始化视图
     */
    private void initViews() {
        recyclerView = rootView.findViewById(R.id.recycler_view);
    }

    private void setupRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MessageAdapter();
        recyclerView.setAdapter(adapter);
        
        adapter.setOnItemClickListener(position -> {
            Message message = adapter.getItem(position);
            openChatActivity(message);
        });
    }
    
    /**
     * 打开聊天页面
     */
    private void openChatActivity(Message message) {
        Intent intent = new Intent(getActivity(), ChatActivity.class);
        intent.putExtra(ChatActivity.EXTRA_MESSAGE, message);
        startActivityForResult(intent, REQUEST_CHAT);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CHAT && resultCode == Activity.RESULT_OK && data != null) {
            // 更新消息列表中的对应消息
            Message updatedMessage = data.getParcelableExtra(ChatActivity.EXTRA_UPDATED_MESSAGE);
            if (updatedMessage != null) {
                for (int i = 0; i < messages.size(); i++) {
                    Message message = messages.get(i);
                    if (message.getId() == updatedMessage.getId()) {
                        messages.set(i, updatedMessage);
                        break;
                    }
                }
                // 刷新消息列表
                adapter.setData(messages);
            }
        }
    }
    
    /**
     * 加载消息数据
     */
    private void loadMessages() {
        // 模拟从远程获取消息数据
        messages.clear();
        
        messages.add(new Message(1, "系统通知", "您的订单已发货，请注意查收！", "2023-05-01 14:30", "https://via.placeholder.com/50", true));
        messages.add(new Message(2, "支农宝客服", "您好，您购买的农产品有什么问题吗？", "2023-04-30 09:15", "https://via.placeholder.com/50", true));
        messages.add(new Message(3, "张三的水果店", "您好，收到货了吗？水果新鲜吗？", "2023-04-29 16:45", "https://via.placeholder.com/50", false));
        messages.add(new Message(4, "李四农资", "农资已经准备好了，什么时候来取货？", "2023-04-28 11:30", "https://via.placeholder.com/50", false));
        messages.add(new Message(5, "王五种植基地", "您要的有机蔬菜刚采摘完，明天就能发货！", "2023-04-27 15:20", "https://via.placeholder.com/50", false));
        
        adapter.setData(messages);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        rootView = null;
    }
} 